/**
 * @(#)Main.java 2011-8-24 Copyright 2011 it.kedacom.com, Inc. All rights
 *               reserved.
 */

package com.kedacom.xmpp.demo.oneserver.chat;

import java.util.Collection;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smackx.ChatState;
import org.jivesoftware.smackx.packet.ChatStateExtension;



 /**
  * (用一句话描述类的主要功能)
  * @author 何霄刚  
  * @date 2011-12-20
  */
 
public class User1Client {

	/**
	 * (用一句话描述方法的主要功能)
	 * @param args
	 * @throws XMPPException
	 * @throws InterruptedException
	 */

	public static void main(String[] args) throws XMPPException, InterruptedException {
		String SERVER = "kedacom.com", RESOURCE = "java", USER = "hexiaogang", PASS = "888";
		Connection conn = null;

		// 登录
		conn = new XMPPConnection(SERVER);
		conn.connect();
		conn.login(USER, PASS, RESOURCE);
		System.out.println("hexiaogang已登录!");
		// 设置出席状态－在线
		Presence p = new Presence(Presence.Type.available);
		p.setStatus("在线");
		conn.sendPacket(p);

		// 获取我的花名册
		final Roster roster = conn.getRoster();
		System.out.println("=======================我的花名册：=======================");
		for (RosterEntry rosterEntry : roster.getEntries()) {
			System.out.print("name: " + rosterEntry.getName() + ",jid: " + rosterEntry.getUser() + ",subscription: " + rosterEntry.getType().toString());
			System.out.println("");
		}
		System.out.println("=======================我的花名册：=======================");
		
		// 监听联系人出席状态
		roster.addRosterListener(new RosterListener() {
			@Override
			public void entriesAdded(Collection<String> arg0) {
				for(String s : arg0){
					System.out.println("Entries Added: " + s);
				}
			}

			@Override
			public void entriesDeleted(Collection<String> arg0) {
				for(String s : arg0){
					System.out.println("Entries Deleted: " + s);
				}
			}

			@Override
			public void entriesUpdated(Collection<String> arg0) {
				for(String s : arg0){
					System.out.println("Entries Updated: " + s);
				}
			}

			@Override
			public void presenceChanged(Presence presence) {
				System.out.println("Presence changed: " + presence);
			}
		});
		
		// 创建分组
		String groupName = "同事";
		if(roster.getGroup(groupName)==null){
			roster.createGroup(groupName);
		}
		// 添加联系人
		String contactName = "kongchun@kedacom.com";
		RosterEntry entry = roster.getEntry(contactName);
		if(entry!=null){
			roster.removeEntry(entry);
		}
		roster.createEntry(contactName, "孔纯", new String[]{"同事"});
		
		// 改变出席状态－离线
		p.setMode(Presence.Mode.away);
		p.setStatus("离开");
		conn.sendPacket(p);
		
		// 订阅联系人出席
		Presence p1 = new Presence(Presence.Type.available);
		p1.setType(Presence.Type.subscribe);
		p1.setTo(contactName);
		conn.sendPacket(p1);
		
		ChatManager chatmanager = conn.getChatManager();

		Chat newChat = chatmanager.createChat("kongchun@kedacom.com", new MessageListener() {

			public void processMessage(org.jivesoftware.smack.Chat chat, Message message) {
				System.out.println("接收到信息: " + message.getBody());

			}
		});
		// 主动发信息
		// 发送给test1初始文本信息,附带正在输入的状态
		Message mess = new Message();
		mess.addExtension(new ChatStateExtension(ChatState.composing));
		newChat.sendMessage(mess);
		Thread.currentThread().sleep(1000);

		// 发送给test1初始文本信息,附带输入状态
		mess = new Message();
		mess.addExtension(new ChatStateExtension(ChatState.active));
		mess.setBody("你好!");
		newChat.sendMessage(mess);
		Thread.currentThread().sleep(1000);

		// 发送给test1初始文本信息,附带暂停输入的状态
		mess = new Message();
		mess.addExtension(new ChatStateExtension(ChatState.paused));
		newChat.sendMessage(mess);

		Thread.currentThread().suspend();
	}

}
